package com.bz.xtcx.manager.utils;

import com.linkage.netmsg.server.AnswerBean;
import com.linkage.netmsg.server.ReceiveMsg;
import com.linkage.netmsg.server.ReturnMsgBean;
import com.linkage.netmsg.server.UpMsgBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class SmsReceiveWorker extends ReceiveMsg {
    private final Logger logger = LoggerFactory.getLogger(SmsReceiveWorker.class);

    @Override
    public void getAnswer(AnswerBean answerBean) {
        super.getAnswer(answerBean);
        // 序列Id
        String seqIdString = answerBean.getSeqId();
        // 短信状态 ,0表示提交至API平台成功
        int status = answerBean.getStatus();
        // 下行短信ID，用来唯一标识一条下行短信
        String msgId = answerBean.getMsgId();
        if (0 == status) {
            logger.info("序列[" + seqIdString + "],ID[" + msgId + "],短信提交到API平台成功,状态[" + status + "-" + getAnswerStatus(String.valueOf(status)) + "]");
        } else {
            logger.error("序列[" + seqIdString + "],ID[" + msgId + "],短信提交到API平台失败,状态[" + status + "-" + getAnswerStatus(String.valueOf(status)) + "]");
        }
    }

    @Override
    public void getUpMsg(UpMsgBean pMsgBean) {
        super.getUpMsg(pMsgBean);
    }

    @Override
    public void getReturnMsg(ReturnMsgBean returnMsgBean) {
        super.getReturnMsg(returnMsgBean);
        String sequenceId = returnMsgBean.getSequenceId();
        /* 短信的msgId */
        String msgId = returnMsgBean.getMsgId();
        /* 发送号码 */
        String sendNum = returnMsgBean.getSendNum();
        /* 接收号码 */
        String receiveNum = returnMsgBean.getReceiveNum();
        /* 短信提交时间 */
        String submitTime = returnMsgBean.getSubmitTime();
        /* 短信下发时间 */
        String sendTime = returnMsgBean.getSendTime();
        /* 短信状态 */
        String msgStatus = returnMsgBean.getMsgStatus();
        /* 短信错误代码 */
        int msgErrStatus = returnMsgBean.getMsgErrStatus();

        //此处加入接收短信回执的处理代码
        logger.info("序列[" + sequenceId + "],ID[" + msgId + "],短信回执信息: ");
        logger.info("发送号码: " + sendNum);
        logger.info("接收号码: " + receiveNum);
        logger.info("短信提交时间: " + submitTime);
        logger.info("短信下发时间: " + sendTime);
        logger.info("短信状态: " + msgStatus + "-" + getReturnMsgStatus(msgStatus));
        logger.info("短信错误代码: " + msgErrStatus + "-" + getReturnMsgErrStatus(String.valueOf(msgErrStatus)));
    }

    private String getAnswerStatus(String status) {
        Map<String, String> statusMap = new HashMap<String, String>();
        statusMap.put("0", "成功");
        statusMap.put("1", "系统错误");
        statusMap.put("2", "帐号错误");
        statusMap.put("3", "密码错误");
        statusMap.put("4", "连接数超过限制");
        statusMap.put("5", "秒发送条目数超过限制");
        statusMap.put("6", "目的号码受限制");
        statusMap.put("7", "网络错误");
        statusMap.put("8", "月发送条目数超过限制");
        statusMap.put("9", "客户端关闭连接");
        statusMap.put("10", "短信网关关闭连接");
        statusMap.put("11", "超时退出");
        statusMap.put("12", "连接数据库错误");
        statusMap.put("13", "连接短信网关错误");
        statusMap.put("14", "非法发送内容");
        statusMap.put("15", "非法发送时间");
        statusMap.put("16", "socket连接已经关闭");
        statusMap.put("23", "发送号码前缀错误");
        return WzStrUtil.defaultString(statusMap.get(status));
    }

    private String getReturnMsgStatus(String status) {
        Map<String, String> statusMap = new HashMap<String, String>();
        statusMap.put("DELIVRD", "短消息转发成功");
        statusMap.put("EXPIRED", "短消息超过有效期");
        statusMap.put("DELETED", "短消息已经被删除");
        statusMap.put("UNDELIV", "短消息是不可转发的");
        statusMap.put("ACCEPTD", "短消息已经被最终用户接收");
        statusMap.put("UNKNOWN", "未知短消息状态");
        statusMap.put("REJECTD", "短消息被拒绝");
        return WzStrUtil.defaultString(statusMap.get(status));
    }

    private String getReturnMsgErrStatus(String status) {
        Map<String, String> statusMap = new HashMap<String, String>();
        statusMap.put("0", "成功");
        statusMap.put("1", "用户不能通信");
        statusMap.put("2", "用户忙");
        statusMap.put("3", "终端无此部件号");
        statusMap.put("4", "非法用户");
        statusMap.put("5", "用户在黑名单内");
        statusMap.put("6", "系统错误");
        statusMap.put("7", "用户内存满");
        statusMap.put("8", "非信息终端");
        statusMap.put("9", "数据错误");
        statusMap.put("10", "数据丢失");
        statusMap.put("999", "未知错误");
        return WzStrUtil.defaultString(statusMap.get(status));
    }
}
